package com.ssm.service.commonService;

import com.github.abel533.entity.Example;
import com.github.abel533.entity.Example.Criteria;
import com.github.abel533.mapper.Mapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.ssm.pojo.BasePojo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Date;
import java.util.List;

public abstract class CommonService<T extends BasePojo> {

	//spring4.0支持泛型注入
	@Autowired
	private Mapper<T> mapper;

	private Class<T> clazz;

	@SuppressWarnings("unchecked")
	public CommonService(){
		Type type = getClass().getGenericSuperclass();
		ParameterizedType pType = (ParameterizedType)type;
		clazz = (Class<T>)pType.getActualTypeArguments()[0];
	}
	
	//根据id查询
	public T queryById(Long id){
		T t = mapper.selectByPrimaryKey(id);
		return t;
	}
	
	public List<T> queryListByWhere(T t){
		List<T> list = this.mapper.select(t);
		return list;
	}
	
	public List<T> queryAll(){
		List<T> list = this.mapper.select(null);
		return list;
	}
	
	public Integer queryCountByWhere(T t){
		int count = mapper.selectCount(t);
		return count;
	}
	
	//根据条件分页查询
	public PageInfo<T> queryListByPage(Integer page, Integer rows, T t){
		PageHelper.startPage(page,rows);
		List<T> list = mapper.select(t);
		PageInfo<T> pageInfo = new PageInfo<T>(list);
		return pageInfo;
	}
	
	public T queryOne(T t){
		T one = mapper.selectOne(t);
		return one;
	}
	
	public Integer save(T t){
		t.setCreateTime(new Date());
		t.setUpdateTime(t.getCreateTime());
		int count = mapper.insert(t);
		return count;
	}
	
	public Integer saveSelective(T t){
		t.setCreateTime(new Date());
		t.setUpdateTime(t.getCreateTime());
		int count = mapper.insertSelective(t);
		return count;
	}
	
	public Integer updateById(T t){
		t.setUpdateTime(new Date());
		int count = mapper.updateByPrimaryKeySelective(t);
		return count;
	}
	
	public Integer updateByIdSelective(T t){
		t.setUpdateTime(new Date());
		int count = mapper.updateByPrimaryKeySelective(t);
		return count;
	}
	
	public Integer deleteById(Long id){
		int count = mapper.deleteByPrimaryKey(id);
		return count;
	}
	
	public Integer deleteByIds(List<Object> ids){
		Example example = new Example(clazz);
		Criteria criteria = example.createCriteria();
		criteria.andIn("id",ids);
		int count = mapper.deleteByExample(example);
		return count;
	}

	public PageInfo<T> queryListByPage(T t, Integer page,
                                       Integer rows, String order) throws Exception {
		PageHelper.startPage(page, rows);

        Example example = new Example(clazz);

        // 设置排序字段
        if (StringUtils.isNotBlank(order)) {
            example.setOrderByClause(order);
        }

        // 获取查询条件
        Criteria criteria = example.createCriteria();

        if (t != null) {
            // 获取t的所有字段
            Field[] fields = t.getClass().getDeclaredFields();
            for (Field field : fields) {
                // 设置权限，能够读取所有的字段的值
                field.setAccessible(true);

                // 判断字段的值是否为null
                if (field.get(t) != null) {
                    // 设置查询条件
                    criteria.andEqualTo(field.getName(), field.get(t));
                }
            }
        }

        // 开始执行查询
        List<T> list = this.mapper.selectByExample(example);

        // 创建pageInfo
        PageInfo<T> pageInfo = new PageInfo<T>(list);

        return pageInfo;
	}

	

}
